﻿@page "/identity/users"
@attribute [Authorize]
@using Microsoft.AspNetCore.Authorization
@using Microsoft.Extensions.Localization
@using Volo.Abp.Application.Dtos
@using Volo.Abp.Identity.Localization
@using Volo.Abp.PermissionManagement.Blazor.Components
@inherits UserManagementBase
@inject IStringLocalizer<IdentityResource> L

@* ************************* PAGE HEADER ************************* *@
<Row>
    <Column ColumnSize="ColumnSize.Is6">
        <h1>@L["Users"]</h1>
    </Column>
    <Column ColumnSize="ColumnSize.Is6">
        <Paragraph Alignment="TextAlignment.Right">
            <Button Color="Color.Primary" Clicked="OpenCreateModalAsync">@L["NewUser"]</Button>
        </Paragraph>
    </Column>
</Row>

@* ************************* DATA GRID ************************* *@
<DataGrid TItem="IdentityUserDto"
          Data="Entities"
          ReadData="OnDataGridReadAsync"
          TotalItems="TotalCount"
          ShowPager="true"
          PageSize="PageSize">
    <DataGridColumns>
        <DataGridColumn Width="150px" TItem="IdentityUserDto" Field="@nameof(IdentityUserDto.UserName)" Sortable="false" Caption="@L["Actions"].Value">
            <DisplayTemplate>
                <Dropdown>
                    <DropdownToggle Color="Color.Primary">
                        @L["Actions"]
                    </DropdownToggle>
                    <DropdownMenu>
                        <DropdownItem Clicked="() => OpenEditModalAsync(context.As<IdentityUserDto>().Id)">@L["Edit"]</DropdownItem>
                        <DropdownItem Clicked="() => PermissionManagementModal.OpenAsync(PermissionProviderName,context.As<IdentityUserDto>().Id.ToString())">@L["Permissions"]</DropdownItem>
                        <DropdownDivider/>
                        <DropdownItem Clicked="() => DeleteEntityAsync(context.As<IdentityUserDto>())">@L["Delete"]</DropdownItem>
                    </DropdownMenu>
                </Dropdown>
            </DisplayTemplate>
        </DataGridColumn>
        <DataGridColumn TItem="IdentityUserDto" Field="@nameof(IdentityUserDto.UserName)" Caption="@L["UserName"].Value">
            <DisplayTemplate>
                @(context.As<IdentityUserDto>().UserName)
            </DisplayTemplate>
        </DataGridColumn>
        <DataGridColumn TItem="IdentityUserDto" Field="@nameof(IdentityUserDto.Email)" Caption="@L["Email"].Value">
            <DisplayTemplate>
                @(context.As<IdentityUserDto>().Email)
            </DisplayTemplate>
        </DataGridColumn>
        <DataGridColumn TItem="IdentityUserDto" Field="@nameof(IdentityUserDto.PhoneNumber)" Caption="@L["PhoneNumber"].Value">
            <DisplayTemplate>
                @(context.As<IdentityUserDto>().PhoneNumber)
            </DisplayTemplate>
        </DataGridColumn>
    </DataGridColumns>
</DataGrid>

@* ************************* CREATE MODAL ************************* *@
<Modal @ref="CreateModal">
    <ModalBackdrop />
    <ModalContent IsCentered="true">
        <ModalHeader>
            <ModalTitle>@L["NewUser"]</ModalTitle>
            <CloseButton Clicked="CloseCreateModalAsync" />
        </ModalHeader>
        <ModalBody>
            <Tabs @bind-SelectedTab="@_createModalSelectedTab">
                <Items>
                    <Tab Name="UserInformations">@L["UserInformations"]</Tab>
                    <Tab Name="Roles">@L["Roles"]</Tab>
                </Items>
                <Content>
                    <TabPanel Name="UserInformations">
                        <Field>
                            <FieldLabel>@L["DisplayName:UserName"]</FieldLabel>
                            <TextEdit @bind-text="NewEntity.UserName" />
                        </Field>
                        <Field>
                            <FieldLabel>@L["DisplayName:Name"]</FieldLabel>
                            <TextEdit @bind-text="NewEntity.Name" />
                        </Field>
                        <Field>
                            <FieldLabel>@L["DisplayName:Surname"]</FieldLabel>
                            <TextEdit @bind-text="NewEntity.Surname" />
                        </Field>
                        <Field>
                            <FieldLabel>@L["DisplayName:Password"]</FieldLabel>
                            <TextEdit Role="TextRole.Password" @bind-text="NewEntity.Password" />
                        </Field>
                        <Field>
                            <FieldLabel>@L["DisplayName:Email"]</FieldLabel>
                            <TextEdit @bind-text="NewEntity.Email" />
                        </Field>
                        <Field>
                            <FieldLabel>@L["DisplayName:PhoneNumber"]</FieldLabel>
                            <TextEdit @bind-text="NewEntity.PhoneNumber" />
                        </Field>
                        <Field>
                            <Check TValue="bool" @bind-checked="@NewEntity.LockoutEnabled">@L["DisplayName:LockoutEnabled"]</Check>
                        </Field>
                        <Field>
                            <Check TValue="bool" @bind-checked="@NewEntity.TwoFactorEnabled">@L["DisplayName:TwoFactorEnabled"]</Check>
                        </Field>
                    </TabPanel>
                    <TabPanel Name="Roles">
                        @if (NewUserRoles != null)
                        {
                            @foreach (var role in NewUserRoles)
                            {
                                <Field>
                                    <input type="hidden" @bind-value="@role.Name" />
                                    <Check TValue="bool" @bind-checked="@role.IsAssigned">@role.Name</Check>
                                </Field>
                            }
                        }
                    </TabPanel>
                </Content>
            </Tabs>
        </ModalBody>
        <ModalFooter>
            <Button Color="Color.Secondary" Clicked="CloseCreateModalAsync">@L["Cancel"]</Button>
            <Button Color="Color.Primary" Clicked="CreateEntityAsync">@L["Save"]</Button>
        </ModalFooter>
    </ModalContent>
</Modal>

@* ************************* EDIT MODAL ************************* *@
<Modal @ref="EditModal">
    <ModalBackdrop />
    <ModalContent IsCentered="true">
        <ModalHeader>
            <ModalTitle>role</ModalTitle>
            <CloseButton Clicked="CloseEditModalAsync" />
        </ModalHeader>
        <ModalBody>
            <input type="hidden" name="ConcurrencyStamp" @bind-value="EditingEntity.ConcurrencyStamp" />

            <Tabs @bind-SelectedTab="@_editModalSelectedTab">
                <Items>
                    <Tab Name="UserInformations">@L["UserInformations"]</Tab>
                    <Tab Name="Roles">@L["Roles"]</Tab>
                </Items>
                <Content>
                    <TabPanel Name="UserInformations">
                        <Field>
                            <FieldLabel>@L["DisplayName:UserName"]</FieldLabel>
                            <TextEdit @bind-text="EditingEntity.UserName" />
                        </Field>
                        <Field>
                            <FieldLabel>@L["DisplayName:Name"]</FieldLabel>
                            <TextEdit @bind-text="EditingEntity.Name" />
                        </Field>
                        <Field>
                            <FieldLabel>@L["DisplayName:Surname"]</FieldLabel>
                            <TextEdit @bind-text="EditingEntity.Surname" />
                        </Field>
                        <Field>
                            <FieldLabel>@L["DisplayName:Password"]</FieldLabel>
                            <TextEdit Role="TextRole.Password" @bind-text="EditingEntity.Password" />
                        </Field>
                        <Field>
                            <FieldLabel>@L["DisplayName:Email"]</FieldLabel>
                            <TextEdit @bind-text="EditingEntity.Email" />
                        </Field>
                        <Field>
                            <FieldLabel>@L["DisplayName:PhoneNumber"]</FieldLabel>
                            <TextEdit @bind-text="EditingEntity.PhoneNumber" />
                        </Field>
                        <Field>
                            <Check TValue="bool" @bind-checked="EditingEntity.LockoutEnabled">@L["DisplayName:LockoutEnabled"]</Check>
                        </Field>
                        <Field>
                            <Check TValue="bool" @bind-checked="EditingEntity.TwoFactorEnabled">@L["DisplayName:TwoFactorEnabled"]</Check>
                        </Field>
                    </TabPanel>
                    <TabPanel Name="Roles">
                        @if (EditUserRoles != null)
                        {
                            @foreach (var role in EditUserRoles)
                            {
                                <Field>
                                    <input type="hidden" @bind-value="@role.Name" />
                                    <Check TValue="bool" @bind-checked="@role.IsAssigned">@role.Name</Check>
                                </Field>
                            }
                        }
                    </TabPanel>
                </Content>
            </Tabs>
        </ModalBody>
        <ModalFooter>
            <Button Color="Color.Secondary" Clicked="CloseEditModalAsync">@L["Cancel"]</Button>
            <Button Color="Color.Primary" Clicked="UpdateEntityAsync">@L["Save"]</Button>
        </ModalFooter>
    </ModalContent>
</Modal>

<PermissionManagementModal @ref="PermissionManagementModal"/>
